//
//  BSDetailViewController.m
//  ZhiBoYuan
//
//  Created by apple on 2018/4/16.
//  Copyright © 2018年 apple. All rights reserved.
//

#import "BSDetailViewController.h"
#import "ABCell.h"
#import "AdetailViewController.h"

#import "PaperViewControllerBuild.h"

#import "TaoLunVC.h"

static NSString * identB = @"ABCell";



@interface BSDetailViewController ()
<
UITableViewDataSource,
UITableViewDelegate
>

//@property (nonatomic, strong) UITableView *tableView;

@property(nonatomic,strong)AxcEmptyDataTableView *tableView;

@property (nonatomic, assign) NSInteger pageIndex;

@property (nonatomic, strong)NSMutableArray *listArray;

@end

@implementation BSDetailViewController

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
   [self loadData];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    _listArray = [NSMutableArray array];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self loadData];
    
     [self.view addSubview:self.tableView];
//    self.navigationItem.rightBarButtonItem= [[UIBarButtonItem alloc]initWithTitle:@"发表" style:(UIBarButtonItemStyleDone) target:self action:@selector(taolunAction)];
    
}

-(void)taolunAction{
    TaoLunVC *taolun = [[TaoLunVC alloc]init];
    taolun.course_id = _renwuId;
//    taolun.fresh = ^(Task *task) {
//         _FaBiaoNum = taolun.task.pineLuncount;
//        [self.tableView reloadData];
//    };
   
    [self.navigationController pushViewController:taolun animated:YES];
    
}

-(void)loadData{
    
    if (!_renwuId) {
        return;
    }
    
    SGParameter *parameter  = [[SGParameter alloc] init];
    
    parameter.body = @{@"task_id":_renwuId
                       };
    
    NSString *lastUrl  = @"g=Client&m=Task&a=coursePaperList";
    
    [SGHttpTool POST:[NSString stringWithFormat:@"%@%@",HTTPP,lastUrl] parameter:parameter sucess:^(id responseObject) {
        
        [_listArray removeAllObjects];
        
        NSDictionary *dic = responseObject;

        NSLog(@"%@",dic);
        
        if ([dic[@"status"] intValue] == 200) {
            
            for (NSDictionary *dicc in dic[@"data"][@"course_paper_list"])
            {
                [_listArray addObject:dicc];
            }
            
            [self.tableView reloadData];
        }else{
            [MBProgressHUD showTextOnly:self.view message:dic[@"msg"] delay:2.0];
        }
        
        
    } failure:^(NSError *error) {
        
        NSLog(@"%@",error);
        
    }];
    
}


#pragma mark -- UITableViewDataSource,UITableViewDelegate

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    if ([_FaBiaoNum intValue] > 0 && _listArray.count > 0) {
        return 2;
    }else if(([_FaBiaoNum intValue] > 0 && _listArray.count == 0) || ([_FaBiaoNum intValue] == 0 && _listArray.count > 0)){
        return 1;
    }else{
        return 0;
    }
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    if (section == 0 &&  [_FaBiaoNum intValue] > 0) {
        return 1;
    }else{
         return _listArray.count;
    }
    
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        ABCell *cell = [tableView dequeueReusableCellWithIdentifier:identB forIndexPath:indexPath];
    
    
        cell.Kebadage.hidden = NO;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.backgroundColor = [UIColor clearColor];
        //    cell.textLabel.text = [NSString stringWithFormat:@"%ld",indexPath.row];
        //    zjsDataBaseLesson *data = self.lessonArray[indexPath.row];
        //    [cell loadData:data];
//
    
    if (indexPath.section == 0 && [_FaBiaoNum intValue] > 0) {
        cell.titleLabel.text = @"讨论帖";
        [cell.headImage setImage:[UIImage imageNamed:@"发表123"]];
        cell.browseNum.hidden = YES;
        cell.teacherLabel.hidden = YES;
        cell.priceNum.hidden = YES;
        cell.freeLabel.hidden = YES;
        cell.Kebadage.hidden = YES;
        
        return cell;
    }else{
        
        cell.titleLabel.text = [_listArray[indexPath.row] valueForKey:@"name"];
        
        cell.teacherLabel.text = [_listArray[indexPath.row] valueForKey:@"teacher_name"] ;
        
        if ([[_listArray[indexPath.row] valueForKey:@"is_course"] intValue] == 1) {
            //课程
            [cell.Kebadage setImage:[UIImage imageNamed:@"课"]];
            NSString *url1 = [_listArray[indexPath.row] valueForKey:@"image_url"];
            [cell.headImage sd_setImageWithURL:[NSURL URLWithString:url1] placeholderImage:[UIImage imageNamed:@"0009"]];
            cell.priceNum.text = [NSString stringWithFormat:@"%@学豆",[_listArray[indexPath.row] valueForKey:@"price"]];
            cell.browseNum.text = [NSString stringWithFormat:@"学习人数:%@",[_listArray[indexPath.row] valueForKey:@"view_num"]];
        }else{
            //试卷
            [cell.Kebadage setImage:[UIImage imageNamed:@"试"]];
            cell.priceNum.hidden = YES;
            cell.browseNum.hidden = YES;
            cell.freeLabel.hidden = YES;
            
        }
        
        return cell;
    }


}

//- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
//    return SNRealValue(100);
//}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
//    if (_listArray.count-1 <= indexPath.row) {
//        return;
//    }
    
    if (indexPath.section == 0 && [_FaBiaoNum intValue] >  0) {
        [self taolunAction];
    }else{
        
        
        if ([[_listArray[indexPath.row] valueForKey:@"is_course"] intValue] == 1) {
            //课程
            [Helper keChengDetailFromVC:self course_id:[_listArray[indexPath.row] valueForKey:@"id"]];
            
        }else{
            //试卷
            PaperViewController * paper;
            if ([[_listArray[indexPath.row] valueForKey:@"had_do"] intValue] == 0) {//没做过
                paper = [PaperViewControllerBuild createIntelligentViewController];
                paper.answerType = @"paper_id";
                paper.url = PaperQuestionList;
                paper.ID = [_listArray[indexPath.row] valueForKey:@"id"];
                
                
            }else{//做过
                paper = [PaperViewControllerBuild createIntelligentAllAnalyViewControllerWithData:@{}];
                paper.ID = [_listArray[indexPath.row] valueForKey:@"id"];
                paper.answerType = @"paper_id";
                paper.url = PaperQuestionList;
                
            }
            [ZYCurrentViewController().navigationController pushViewController:paper animated:YES];
            
        }

    }

    
  
    
}


#pragma makr -- property
- (NSMutableArray *)dataArray {
    if (!_listArray) {
        _listArray = [[NSMutableArray alloc] init];
    }
    return _listArray;
}

- (UITableView *)tableView {
    if (!_tableView) {
        AxcEmptyDataTableView *  tableView = [[AxcEmptyDataTableView alloc] initWithFrame:CGRectMake(0, NAVIGATION_BAR_HEIGHT, ScreenWidth, Screenheight- NAVIGATION_BAR_HEIGHT) style:UITableViewStylePlain];
        tableView.backgroundColor = [UIColor clearColor];
        tableView.delegate        = self;
        tableView.dataSource      = self;
        //        tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
        
        tableView.estimatedRowHeight = 44.0f;//推测高度，必须有，可以随便写多少
        
        tableView.rowHeight = UITableViewAutomaticDimension;//iOS8之后默认就是这个值，可以省略
        
        //        self.extendedLayoutIncludesOpaqueBars = YES;
        
        if (@available(iOS 11.0, *)) {
            tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        } else {
            self.automaticallyAdjustsScrollViewInsets = NO;
        }
        tableView.contentInset = UIEdgeInsetsMake(0, 0, 49, 0);
        tableView.scrollIndicatorInsets = _tableView.contentInset;
        
        //         [tableView registerNib:[UINib nibWithNibName:identC bundle:nil] forCellReuseIdentifier:identC];
        
        [tableView registerNib:[UINib nibWithNibName:identB bundle:nil] forCellReuseIdentifier:identB];
        
        [tableView setTableFooterView:[[UIView alloc] initWithFrame:CGRectZero]];

        
        
        self.placeholderView.frame = tableView.bounds;
        
        // 设置空集时候展示的View
        tableView.axcUI_placeHolderView = self.placeholderView;
        
        //  默认YES 自带渐入渐出动画效果，下边这个参数可以关闭
        tableView.axcUI_placeHolderViewAnimations = NO;
        
        // 占位View的“重新加载”按钮的Block回调:
        __weak typeof(self) WeakSelf = self;
        
        self.placeholderView.clickLoadBtnBlock = ^(id type) {
            //        WeakSelf.dataArray = nil;
            [WeakSelf loadData];
            
            
        };
        
        _tableView = tableView;
        
    }
    return _tableView;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
